package com.wangijun;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 运行即可
 */
public class Main {

    public static void main(String[] args) throws SQLException {

        String baseUrl = "http://xzqh.mca.gov.cn/map";

        String bodyHtml = HttpUtil.createGet(baseUrl).execute().body();

        /*Db use = Db.use();

        Entity entity = Entity.create("sys_organ");*/

        Document parse = Jsoup.parse(bodyHtml);

        Elements select = parse.select("script");
        String data = select.last().data();
        String arr = data.split("\n")[1].replace("var json = ", "").replace(";", "");

        List<PyA2> ShengJi = JSON.parseArray(arr, PyA2.class);

        ArrayList<JSONObject> shengJsonList = new ArrayList<>();

        for (PyA2 sheng : ShengJi) {
            JSONObject shengJson = new JSONObject();
            long snowflakeNextId = IdUtil.getSnowflakeNextId();
            /*use.insert(
                    entity
                            .set("id", snowflakeNextId)
                            .set("pid",0L)
                            .set("organ_no",sheng.quHuaDaiMa)
                            .set("name",sheng.shengji)
            );*/
            shengJson.put("organNo", sheng.quHuaDaiMa);
            shengJson.put("name", sheng.shengji);

            String dishi = HttpUtil.createPost("http://xzqh.mca.gov.cn/selectJson")
                    .form("shengji", sheng.getShengji())
                    .execute().body();
            List<PyA2> diShiList = JSON.parseArray(dishi, PyA2.class);
            ArrayList<JSONObject> diJsonList = new ArrayList<>();
            for (PyA2 di : diShiList) {
                JSONObject diJson = new JSONObject();
                long snowflakeNextId2 = IdUtil.getSnowflakeNextId();
                /*use.insert(
                        entity
                                .set("id", snowflakeNextId2)
                                .set("pid",snowflakeNextId)
                                .set("organ_no",di.quHuaDaiMa)
                                .set("name",di.diji)
                );*/
                diJson.put("organNo", di.quHuaDaiMa);
                diJson.put("name", di.diji);

                String xianQuStr = HttpUtil.createPost("http://xzqh.mca.gov.cn/selectJson")
                        .form("shengji", di.getShengji())
                        .form("diji", di.diji)
                        .execute().body();

                List<PyA2> xianQuList = JSON.parseArray(xianQuStr, PyA2.class);
                ArrayList<JSONObject> xianQuJsonList = new ArrayList<>();
                for (PyA2 xianQu : xianQuList) {
                    JSONObject xianQuJson = new JSONObject();
                    long snowflakeNextId3 = IdUtil.getSnowflakeNextId();
                    /*use.insert(
                            entity
                                    .set("id", snowflakeNextId3)
                                    .set("pid",snowflakeNextId2)
                                    .set("organ_no",xianQU.quHuaDaiMa)
                                    .set("name",xianQU.xianji)
                    );*/
                    xianQuJson.put("organNo", xianQu.quHuaDaiMa);
                    xianQuJson.put("name", xianQu.xianji);
                    xianQuJsonList.add(xianQuJson);
                }
                diJson.put("child", xianQuJsonList);
                diJsonList.add(diJson);
            }
            shengJson.put("child", diJsonList);
            shengJsonList.add(shengJson);
        }

        FileUtil.writeString(JSON.toJSONString(shengJsonList), "./local.json", StandardCharsets.UTF_8);
        System.out.println("执行完成");
    }


    // { "cName":"宁县","code":"621026","py":"Ning Xian","jp":"nx","qp":"NingXian"}


    // {"children":[],"diji":"","quHuaDaiMa":"110000","quhao":null,"shengji":"北京市（京）","xianji":""}
    @Data
    static class PyA2 {
        private List<String> children;
        private String diji;
        private String quHuaDaiMa;
        private String quhao;
        private String shengji;
        private String xianji;
    }

}
